﻿
/**
 生产计划删除
 **/
function DelData(plCode,status) {
    var info = undefined;
    $.ajax({
        url: "../Plan/DelPlan",
        data: { plCode: plCode ,status:status},
        dataType: "json",
        type: "POST",
        async: false,
        success: function (data) {
            info = data;
        }
    });
    return info;
}
/**
* 存货选择
 * **/
function chooseProduct() {
    var pType = $("input[ name='plType']").val();
    if (pType == "P-BeiHuo") {
        let url = "../Basic/SelectInventory?isSingle=0";
        InfoOpenPage(url, "存货选择", "95%", "90%");
    }
}
/**
 * 存货参数获取
 * **/
function getChildPar(pars) {
    if (pars != undefined) {
        $("input[ name='proName']").val(pars[0].proName);
        $("input[ name='proCode']").val(pars[0].proCode);
        $("input[ name='spec']").val(pars[0].spec);
        //GetGyCode();
        GetBOMCode();
    }
}

/**
 * 根据存货、客户查询相关工艺
 * **/
function GetGyCode() {
    var proCode = $("input[ name='proCode']").val();
    var cuCode = $("input[ name='cuCode']").val();
    $.ajax({
        url: "../Plan/GetProductGyCode",
        data: { proCode: proCode, cuCode: cuCode },
        type: "post",
        success: function (rsl) {
            if (rsl.result) {
                var data = rsl.data
                var info = data.gyName + "/" + data.gyCode;
                if (data.plCode != "") {
                    info += "/" + data.plCode;
                }
                $("input[ name='gyInfo']").val(info);
                $("input[ name='gyPlan']").val(data.plCode);
                $("input[ name='gyCode']").val(data.gyCode);
            } else {
                layer.msg(rsl.message);
            }
        }
    });
}

/**
 * 根据存货查询相关BOM
 * **/
function GetBOMCode() {
    var proCode = $("input[ name='proCode']").val();
    $.ajax({
        url: "../Plan/GetProductBOMCode",
        data: { proCode: proCode },
        type: "post",
        success: function (rsl) {
            if (rsl.result) {
                var data = rsl.data
                $("input[ name='bomCode']").val(data.bomCode);

                GetBgByBOM(data.bomCode);
            } else {
                $("input[ name='errmsg']").val(rsl.message);  //没有bom不允许保存
                layer.msg(rsl.message);
            }
        }
    });
}


/**
 *选择客户 
 **/
function chooseCustomer() {
    var pType = $("input[ name='plType']").val();
    if (pType == "P-BeiHuo") {
        var url = "../Basic/SelectCustomer?isSingle=1&dataType=Cu";
        InfoOpenPage(url, "客户选择", "95%", "90%");
    }
}
/**
 * 加载获取客户数据信息
 **/
function getCustomPar(pars) {
    if (pars != undefined) {
        $("input[ name='cuName']").val(pars[0].cuName);
        $("input[ name='cuCode']").val(pars[0].cuCode);
    }
}
/**
* 加载计划信息
**/
function loadPlan(dataId) {
    $.ajax({
        url: "../Plan/getPlanInfoByCode",
        data: { plCode: dataId },
        type: "post",
        success: function (rsl) {
            if (rsl.result) {
                $.each(rsl.data[0], function (name, value) {
                    if (name == "zhxBz" || name == "cjCode" || name == "cu_caizhi" || name == "gcName") {
                        $("#" + name + " option[value='" + value + "'] ").attr("selected", true);
                    }
                    else if (name == "orderMemo") {
                        document.getElementById('orderMemo').innerText = value;
                    }
                    else {
                        $("input[ name='" + name + "']").val(value);
                    }
                });

                layui.use('form', function () {
                    var form = layui.form;
                    form.render();
                });
               
                GetBgByBOM(rsl.data[0]["bomCode"]);

            } else {
                layer.msg(rsl.message);
            }
        }
    });
}


/**
 * 打开销售订单
 * **/
function OpenSale() {
    InfoOpenPage("../Plan/SalesInfo", "ERP销售订单", "95%", "90%");
}

/**
 * 获取销售订单数据
 **/
function getSaleOrderPar(pars) {
    //console.log(pars)
    if (pars != undefined) {
        $("input[ name='proName']").val(pars[0].proName);
        $("input[ name='proCode']").val(pars[0].proCode);
        $("input[ name='spec']").val(pars[0].spec);
        $("input[ name='plType']").val("P-Sale");
        $("input[ name='erpOrder']").val(pars[0].erpOrder);
        $("input[ name='erpRowId']").val(pars[0].erpRowId);
        $("input[ name='number']").val(pars[0].number);
        $("input[ name='cuCode']").val(pars[0].cuCode);
        $("input[ name='cuName']").val(pars[0].cuName);
        $("input[ name='jfDate']").val(pars[0].fDate);
        //$("input[ name='orderMemo']").val(pars[0].orderMemo);
        document.getElementById('orderMemo').innerText = pars[0].orderMemo;
        //GetGyCode();
        GetBOMCode();
    }
}

/**
 * 往期订单
 * **/
function ViewPlan() {
    let cuCode = $("input[ name='cuCode']").val();
    let cuName = $("input[ name='cuName']").val();
    let proCode = $("input[ name='proCode']").val();
    let proName = $("input[ name='proName']").val();
    InfoOpenPage("../Plan/RecentPlanInfo?cuCode=" + cuCode + "&proCode=" + proCode + "&cuName=" + cuName + "&proName=" + proName, "往期计划", "95%", "90%");
}

/**
 * 往期订单赋值
 **/
function lastPlanPar(pars) {
    if (pars != undefined) {
        $.each(pars[0], function (name, value) {
            //if (name == "plCode") {
            //    $("input[ name='gyPlan']").val(value);
            //} else if (name == "id") {

            //} else 
            if (name == "erpOrder") {
                
            } else {
                $("input[ name='" + name + "']").val(value);
            }

        });
        //GetGyCode();
        GetBOMCode();

        loadPlanGx(pars[0]["plCode"]);
        loadGx();
    }
}

/**
 * 预览工艺数据
 * **/
function ViewGY() {
    let plCode = $("input[ name='gyPlan']").val();
    let gyCode = $("input[ name='gyCode']").val();
    InfoOpenPage("../Plan/PlanProcess?plCode=" + plCode + "&read=1&gyCode=" + gyCode, "生产工艺(" + gyCode + ")", "95%", "90%");
}


/**
 * 保存生产计划
 * **/
function SavePlan(cData) {

    //console.log(sign)
    if (cData.length > 0) {
        if (!sign) {
            return;
        }
        sign = false;

        $.ajax({
            url: "../Plan/SaveProductPlan",
            data: { datas: JSON.stringify(FormToJson("dForm")), cData: JSON.stringify(cData) },
            type: "post",
            success: function (rsl) {
                if (rsl.result) {
                    layer.msg("计划保存成功");
                } else {
                    layer.msg(rsl.message);
                }

                setTimeout(function () {
                    sign = true;
                }, 3000); //单位是毫秒
                
            }
        });
    } else {
        layer.msg("请选择生产工序！");
        return;
    }
    
}

/**
 * 计划工艺数据保存
 * **/
function SavePlanGYData(formId) {
    var datas = JSON.stringify(FormToJson(formId));
    $.ajax({
        url: "../Plan/SavePlanTechnology",
        data: { datas: datas },
        type: "post",
        success: function (data) {
            if (data.result) {
                layer.msg("保存成功！");
            } else {
                layer.msg(data.message);
            }
        }
    });
}

/**
  * 加载计划工艺数据信息
  * **/
function loadGyFormData(plCode,gxCode) {
    $.ajax({
        url: "../Plan/GetPlanTechnologyForm",
        type: "post",
        data: { plCode: plCode, gxCode: gxCode },
        success: function (data) {
            if (data.result) {
                var datas = data.data;
                layui.use('form', function () {
                    var form = layui.form;
                    form.val('form-filter', datas);
                    form.render();
                });
            }
        }
    });
}
